//
//  YSShareView.m
//  Yueshi
//
//  Created by Northwan on 15/8/8.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//

#import "YSShareView.h"
#import "WXClient.h"
#import "SinaClient.h"
#import "CustomShowView.h"
#import "QzoneClient.h"
#import <TencentOpenAPI/TencentOAuth.h>
#import "YSCustomShareBtn.h"

#define OuthBtnWidth SCREENW/4

@interface YSShareView()
@property (strong, nonatomic)  YSCustomShareBtn *wxView;
@property (strong, nonatomic)  YSCustomShareBtn *wxCircleView;
@property (strong, nonatomic)  YSCustomShareBtn *qzoneView;
@property (strong, nonatomic)  YSCustomShareBtn *sinaView;
@property (assign, nonatomic)  CGFloat currentX;
@end

@implementation YSShareView

-(instancetype)init{
    if(self = [super init]){
        [self setBackgroundColor:PUBLIC_CARD_BG];
        [self setFrame:CGRectMake(0, 0, SCREENW, 135)];
        
        _wxView = [[YSCustomShareBtn alloc]init];
        [_wxView setTag:0];
        [_wxView addTarget:self action:@selector(tapShare:) forControlEvents:UIControlEventTouchUpInside];
        [_wxView setTitle:@"微信" forState:UIControlStateNormal];
        [_wxView setImage:IMG(@"icon_share_wx") forState:UIControlStateNormal];
        [_wxView setFrame:CGRectMake(0, 0, OuthBtnWidth,self.height)];
        
        
        _wxCircleView = [[YSCustomShareBtn alloc]init];
        [_wxCircleView setTag:1];
        [_wxCircleView addTarget:self action:@selector(tapShare:) forControlEvents:UIControlEventTouchUpInside];
        [_wxCircleView setTitle:@"朋友圈" forState:UIControlStateNormal];
        [_wxCircleView setImage:IMG(@"icon_share_wxscene") forState:UIControlStateNormal];
        [_wxCircleView setFrame:CGRectMake(0, 0,OuthBtnWidth ,self.height)];
        
        
        _qzoneView = [[YSCustomShareBtn alloc]init];
        [_qzoneView setTag:2];
        [_qzoneView addTarget:self action:@selector(tapShare:) forControlEvents:UIControlEventTouchUpInside];
        [_qzoneView setTitle:@"空间" forState:UIControlStateNormal];
        [_qzoneView setImage:IMG(@"icon_share_qzone") forState:UIControlStateNormal];
        [_qzoneView setFrame:CGRectMake(0, 0,OuthBtnWidth, self.height)];
        
        
        _sinaView = [[YSCustomShareBtn alloc]init];
        [_sinaView setTag:3];
        [_sinaView addTarget:self action:@selector(tapShare:) forControlEvents:UIControlEventTouchUpInside];
        [_sinaView setTitle:@"微博" forState:UIControlStateNormal];
        [_sinaView setImage:IMG(@"icon_share_sina") forState:UIControlStateNormal];
        [_sinaView setFrame:CGRectMake(0, 0,OuthBtnWidth, self.height)];

    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    _currentX = 0;
    
    if(![WXApi isWXAppInstalled]){
        [self.wxView setHidden:YES];
        [self.wxCircleView setHidden:YES];
    }else{
        [self.wxView setX:_currentX];
        [self.wxCircleView setX:CGRectGetMaxX(self.wxView.frame)];
        self.currentX = CGRectGetMaxX(self.wxCircleView.frame);
        [self addSubview:_wxView];
        [self addSubview:_wxCircleView];
    }
    

    if(![TencentOAuth iphoneQQInstalled]){
        [self.qzoneView setHidden:YES];
    }else{
        [self.qzoneView setX:_currentX];
        self.currentX = CGRectGetMaxX(self.qzoneView.frame);
        [self addSubview:_qzoneView];
    }
    
    if(![WeiboSDK isWeiboAppInstalled]){
        [self.sinaView setHidden:YES];
    }else{
        [self.sinaView setX:_currentX];
        self.currentX = CGRectGetMaxX(self.sinaView.frame);
        [self addSubview:_sinaView];
    }
    
    if(![WXApi isWXAppInstalled]&&![TencentOAuth iphoneQQInstalled]&&![WeiboSDK isWeiboAppInstalled]){
        NSString *tipText = @"没找到可以分享到的平台!";
        CGSize nullTipsSize = [tipText sizeWithFont:SYS_FONT(17)];
        UILabel *label= [[UILabel alloc]initWithFrame:CGRectMake(0,(self.height-nullTipsSize.height)/2, SCREENW, 17)];
        [label setText:tipText];
        [label setFont:SYS_FONT(17)];
        [label setTextColor:HEXColor(@"#333333")];
        [label setTextAlignment:NSTextAlignmentCenter];
        [self addSubview:label];
    }
    
}

-(void)setShareModel:(YSShareModel *)shareModel{
    _shareModel = shareModel;
}

- (void)tapShare:(UIButton *)sender {
    switch (sender.tag) {
        case 0:
            NSLog(@"share to wx");
            [[WXClient sharedClient]sendLinkContentWithURlStr:_shareModel.url andTitle:_shareModel.title andImageURL:_shareModel.imageUrl andDescription:_shareModel.subTile andScene:0];
            break;
        case 1:
             NSLog(@"share to wx scene");
            [[WXClient sharedClient]sendLinkContentWithURlStr:_shareModel.url andTitle:_shareModel.title andImageURL:_shareModel.imageUrl andDescription:_shareModel.subTile andScene:1];
            break;
        case 2:
             NSLog(@"share to qzone");
            [[QzoneClient sharedClient]sendLinkContentWithURlStr:_shareModel.url andTitle:_shareModel.title andImageURL:_shareModel.imageUrl andDescription:_shareModel.subTile];
            break;
        case 3:{
             NSLog(@"share to sina weibo");

            [[SinaClient ShareSinaClient] sendWeiBo:_shareModel.title description:_shareModel.subTile WithImgUrl:_shareModel.imageUrl webpageUrl:_shareModel.url];
            
//            UIImage *img = [[SDImageCache  sharedImageCache]imageFromDiskCacheForKey:_shareModel.imageUrl];
//            [[SinaClient ShareSinaClient] sendWeiBo:_shareModel.subTile.length >100 ? [_shareModel.subTile substringFromIndex:100] :_shareModel.subTile WithImage:img];
            break;
          }
        default:
            break;
    }
    CustomShowView *superView = (CustomShowView *) self.superview;
    [superView hideWithAnim:YES];
}


@end
